package net.sinodata.smartframework.schedulejob.base;

/**
 * Description: 定时任务工具类
 * 
 * @author: yinliangyun
 * @since: 2021-5-26
 */
public class ScheduleJobUtils {
	
	public static String toJavaPattern(String wildcardString) {
		StringBuffer result = new StringBuffer();
		String[] wcArray = wildcardString.split("\\|");
		for (String wcString : wcArray) {
			if (wcString == null || wcString.trim().length() == 0) {
				continue;
			}
			String[] partArray = wcString.split(":");
			StringBuffer[] partPattern = new StringBuffer[3];
			for (int i = 0; i < 3; i++) {
				partPattern[i] = new StringBuffer();
			}
			if (partArray.length < 3) {
				for (int i = partArray.length; i < 3; i++) {
					partPattern[i].append(".+");
				}
			}
			for (int i = 0; i < partArray.length && i < 3; i++) {
				if (partArray[i] == null || partArray[i].trim().length() == 0) {
					partPattern[i].append(".+");
					continue;
				}
				String ss = partArray[i].trim();
				for (int j = 0; j < ss.length(); j++) {
					char cc = ss.charAt(j);
					switch (cc) {
					case '*':
						partPattern[i].append(".*");
						break;
					case '?':
						partPattern[i].append(".");
						break;
					case '$':
						partPattern[i].append("\\$");
						break;
					case '(':
						partPattern[i].append("\\(");
						break;
					case ')':
						partPattern[i].append("\\)");
						break;
					case '+':
						partPattern[i].append("\\+");
						break;
					case '.':
						partPattern[i].append("\\.");
						break;
					case '[':
						partPattern[i].append("\\[");
						break;
					case ']':
						partPattern[i].append("\\]");
						break;
					case '\\':
						partPattern[i].append("\\\\");
						break;
					case '^':
						partPattern[i].append("\\^");
						break;
					case '{':
						partPattern[i].append("\\{");
						break;
					case '}':
						partPattern[i].append("\\}");
						break;
					case '|':
						partPattern[i].append("\\|");
						break;
					default:
						partPattern[i].append(cc);
					}
				}
			}
			for (int i = 0; i < 2; i++) {
				result.append(partPattern[i]).append("\\:");
			}
			result.append(partPattern[2]).append('|');
		}
		return result.toString();
	}

}
